<?php

namespace app\model;

use think\facade\Cache;
use think\Model;

class Area extends Model
{
    public function children()
    {
        return $this->hasMany(self::class, 'parent', 'adcode')->with(['children'])->field("id,adcode,name,parent");
    }

    public function areaTree(): array
    {
        Cache::clear();
        $tree = $this->with(['children'])->where(['parent' => "100000"])->field("id,adcode,name,parent")->cache("tree_area", 24 * 3600)->select()->toArray();
        if(!file_exists("./area/")) mkdir('/area',0777,true);
//        dd($tree);
        file_put_contents("./area/tree.json", json_encode(['area' => $tree], 320));
        return $tree;
    }
}
